<?php

require_once './database.php';
require_once './arrarr_to_arr.php';
include_once 'log_text.php';
require_once './get_param.php';

function fetch_missing_data($dismiss_notify = false) {
  log_text('fetch_missing_data');
  $params = get_param('missingChecking');
  $output = array();
  foreach ($params as $item) {
    if ($item->enable && ($dismiss_notify || $item->notify)) {
      array_push($output, doit($item, $dismiss_notify));
    }
  }
  return $output;
}

function doit($item, $dismiss_notify) {
  $item_title = $item->key;
  $sql = "SELECT DISTINCT `number` FROM `pims_extention_data` WHERE `itemTitle` = '{$item_title}' AND `status` = 9";
  $res = exec_sql($sql);
  $numbers = arrarr_to_arr($res, 'number');
  $blacklist_arr = implode(',', $item->blacklist);
  $numbers_arr = implode(',', $numbers);
  $ignored_arr = implode(',', $item->ignored);
  $sql = "SELECT `clientName`,`number` FROM `oprec_security_data` WHERE `number` NOT IN ($numbers_arr) AND `disposed` = 0";

  if (count($item->blacklist) > 0) {
    $sql .= " AND `number` NOT IN ($blacklist_arr)";
  }
  if (!$dismiss_notify && count($item->ignored) > 0) {
    $sql .= " AND `number` NOT IN ($ignored_arr)";
  }
  $res = exec_sql($sql);
  $names = arrarr_to_arr($res, 'clientName');
  $numbers = arrarr_to_arr($res, 'number');
  $numbers = implode(',', $numbers);
  $sql_tel = "SELECT `itemValue` FROM `pims_extention_data` WHERE `itemTitle` = '手机号' AND `number` IN ($numbers)";
  $res = exec_sql($sql_tel);
  $phones = arrarr_to_arr($res, 'itemValue');
  return array('itemTitle' => $item_title, 'count' => count($names), 'content' => $names, 'phones' => $phones);
}
